package com.ossjk.qlh.system.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.qlh.system.entity.Role;
import com.ossjk.qlh.system.entity.RoleDepartment;
import com.ossjk.qlh.system.mapper.RoleMapper;
import com.ossjk.qlh.system.service.IRoleDepartmentService;
import com.ossjk.qlh.system.service.IRoleService;
import com.ossjk.qlh.system.vo.RoleVo;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2020-02-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
	@Autowired
	private IRoleDepartmentService iRoleDepartmentService;

	@Override
	public boolean saveVo(RoleVo roleVo) {
		Role role = roleVo;
		// 新增角色
		if (this.save(role)) {
			// 新增角色部门
			RoleDepartment roleDepartment = new RoleDepartment();
			roleDepartment.setRid(roleVo.getId());
			roleDepartment.setDid(roleVo.getDid());
			return iRoleDepartmentService.save(roleDepartment);
		}

		return false;
	}

	@Override
	public RoleVo getVoById(Serializable id) {
		return this.baseMapper.selectVoById(id);
	}

	@Override
	public boolean updateVoById(RoleVo roleVo) {
		Role role = roleVo;
		// 更新角色
		if (this.updateById(role)) {
			// 更新角色部门
			UpdateWrapper<RoleDepartment> updateWrapper = new UpdateWrapper();
			updateWrapper.set("did", roleVo.getDid());
			updateWrapper.eq("rid", roleVo.getId());
			return iRoleDepartmentService.update(updateWrapper);
		}
		return false;
	}

	@Override
	public boolean removeChildrenById(Serializable id) {
		List<Role> roles = queryChildrenById(id);
		QueryWrapper<Role> queryWrapper = new QueryWrapper();
		List<Serializable> ids = new ArrayList<Serializable>();
		for (Role role : roles) {
			ids.add(role.getId());
		}
		// 删除角色
		if (SqlHelper.retBool(baseMapper.deleteBatchIds(ids))) {
			// 删除角色对应的岗位关联
			QueryWrapper<RoleDepartment> queryWrapper2 = new QueryWrapper();
			queryWrapper2.in("rid", ids);
			return iRoleDepartmentService.remove(queryWrapper2);
		}
		return false;
	}

	/**
	 * 查找所有子节点（包含自身）
	 * 
	 * @param id
	 * @return
	 */
	public List<Role> queryChildrenById(Serializable id) {
		List<Role> roles = null;
		Role role = baseMapper.selectById(id);
		if (!CommonUtil.isBlank(role)) {
			roles = new ArrayList();
			roles.add(role);
			queryChildrenByPid(role.getId(), roles);

		}
		return roles;
	}

	/**
	 * 查找所有子节点（不包含自身）
	 * 
	 * @param pid
	 * @param parent
	 * @return
	 */
	public List<Role> queryChildrenByPid(Serializable pid, List<Role> parent) {
		if (CommonUtil.isBlank(parent)) {
			parent = new ArrayList<Role>();
		}
		QueryWrapper<Role> queryWrapper = new QueryWrapper();
		List<Role> children = baseMapper.selectList(queryWrapper.eq("pid", pid));
		if (!CommonUtil.isBlank(children)) {
			parent.addAll(children);
			for (Role role : children) {
				queryChildrenByPid(role.getId(), parent);
			}
		}

		return parent;
	}

	@Override
	public List<String> listUserRidByUid(String uid) {
		 
		return baseMapper.selectUserRidByUid(uid);
	}

}
